import scrapy
from middle import items

class PicSpider(scrapy.Spider):
    name = 'pic'
    # allowed_domains = ['pic.com']
    start_urls = ['https://pic.netbian.com/4kmeinv/index.html']
    host = 'https://pic.netbian.com'

    def parse(self, response):
        res = response.xpath('//ul[@class="clearfix"]/li')
        for i in res:
            item = items.MiddleItem()
            list1 = list()
            url = i.xpath('./a/img/@src').extract_first()
            list1.append(self.host + url)
            title = i.xpath('./a/img/@alt').extract_first()
            item['url'] = list1
            item['title'] = title
            yield item

        # 处理下翻页
        next = response.xpath('//div[@class="page"]/a[text()="下一页"]/@href')
        if next:
            page = next.extract_first()
            yield scrapy.Request(self.host+page,callback=self.parse)

